(function () {

    "use strict";

    var api = {
        getOrderDetailUrl: '/api/order/content',
        chgOrder: '/api/order/chgStatus',
        payNow: '/api/order/payNow',
        afterPay: '/api/order/afterPay'
    };
    sm.controller('wx-order-pay-controller', ['$scope', '$http', 'Toast', 'Modal', function ($scope, $http, Toast, Modal){

        $scope.order = {};
        $scope.address = {};
        $scope.goods = [];
        $scope.message = '';
        $scope.isSubmitting = false;

        var orderId = GetQueryString('orderId');
        var addressId = GetQueryString('addressId');

        var data = {};
        if (orderId != null) {
            angular.extend(data, {orderId: orderId});
        }
        if (addressId != null) {
            angular.extend(data, {addressId: addressId});
        }

        $http.get(api.getOrderDetailUrl, {
            params: data
        }).success(function (res) {
            if (res.state) {
                $scope.order = res.data;
                $scope.address = res.data.address;
                $scope.goods = res.data.goods;
            }
        });

        $scope.add = function() {
            var data = {
                message: $scope.message,
                orderId: orderId
            };

            $scope.isSubmitting = true;
            Toast.loading({title: '支付订单'});
            $http.post(api.payNow, data).success(function (res) {
                $scope.isSubmitting = false;
                Toast.stop();
                if (res.state) {

                    var data = res.data;
                    if (!data.needPay) {
                        location.href = '/order';
                        return false;
                    }

                    var sdk = data.jssdk;
                    wx.chooseWXPay({
                        timestamp: sdk.timeStamp,
                        nonceStr: sdk.nonceStr,
                        package: sdk.package,
                        signType: 'MD5',
                        paySign: sdk.paySign,
                        success: function (res) {
                            afterPay(orderId);
                        }
                    });
                } else {
                    var error = res.error ? res.error : '支付失败';
                    Toast.error({'title': error});
                }
            });
        };

        function afterPay (orderId) {
            $http.post(api.afterPay, {
                'orderId': orderId,
                'status': 'success'
            }).success(function (res) {
                if (res.state) {
                    location.href="/order";
                } else {
                    Modal.alert({
                        title: '操作提示',
                        content: '订单存在异常,请联系客服!'
                    });
                }
            });
        }

        /**
         * 选择地址
         */
        $scope.selectAddr = function () {
            if ($scope.address) {
                // 选择
                location.href = '/address/select?orderId=' + orderId;
            } else {
                // 添加
                location.href = '/address/add?s=confirm';
            }
        };

    }]);
})();

